/* 
 * Author      		: Adnin Devit CF 
 * Date Created		: 31-Maret-2011 
 * Date Modified	: 6-April-2011 
 * Version			: 1.1 
 * */
package jtk.project4.fleet.screen.maintenanceHistory;

import java.awt.FlowLayout;
import java.util.List;

import javax.swing.SortOrder;

//import jtk.project4.fleet.field.IdentificationField;
import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CloseAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.domain.History;
import jtk.project4.fleet.event.EquipmentEvent;
import jtk.project4.fleet.event.MaintenanceHistoryEvent;
import jtk.project4.fleet.field.DataIntervalSettingsField;
//import jtk.project4.fleet.field.MaintenanceHistoryFieldCenter;
import jtk.project4.fleet.field.ButtonsMaintenanceHistoryField;
import jtk.project4.fleet.field.MaintenanceHistoryLaborTableField;
import jtk.project4.fleet.field.MaintenanceHistoryPartTableField;
import jtk.project4.fleet.field.MaintenanceHistoryTable;
import jtk.project4.fleet.field.MaintenanceHistoryMaintenanceTaskTableField;
import jtk.project4.fleet.field.PartInventoryField;
import jtk.project4.fleet.field.PartInventoryTableField;
import jtk.project4.fleet.field.QuantityField;
import jtk.project4.fleet.field.MaintenanceHistoryRepairTableField;
import jtk.project4.fleet.field.TotalsField;
//import jtk.project4.fleet.screen.maintenanceHistory.ButtonsMaintenanceHistoryField;

import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class MaintenanceHistoryView extends FormView{
	
	private TabControl maintenanceHistoryTab;
	private TabItem historyEntries;
	private TabItem maintenanceTask;
	private TabItem repair;
	private TabItem part;
	private TabItem labor;
	private TableField<History> maintenanceHistoryTable;
	
	private FormHeader formHeader;
	
	private PushButton closeButton;
	private PushButton helpButton;
	private Action closeAction;
	
	public void handleEvent(MaintenanceHistoryEvent evt) {
		switch (evt.getType()) {
		case CLOSE:
			close();
			break;
			
		}
	}
	
	public void close() {
		// check if form is changed, if true show dialog
		if (isChanged()) {
			OptionDialog dialog = new OptionDialog(this);
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setTitle("dialog.discard.title");
			dialog.setMessage("dialog.discard.message");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("button.yes", "button.no", "");
			dialog.show();
			if (dialog.getReturnOptionType() == ReturnOptionType.NO) {
				focus();
				return;
			}
		}
		super.close();
	}
	
	private void createAction() {
		//Action = new SubmitAction();
		closeAction = new CloseAction();
		// enable submit button when form is changed
		//setOnChangeAction(closeAction);
	}
	
	private void createTable(){
		createHistoryEntriesTable();
	}
	
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createTable();
		createTabFields();
		createAction();
		createFields();
		createLayout();
	}
	private FormHeader createHeader() {
		formHeader = new FormHeader();
		formHeader.setTitle("Maintenance History.header.form");
		return formHeader;
	}
	private void createFields(){
		helpButton = new PushButton("button.help");
		closeButton = new PushButton(closeAction);
	}
	
	private FormField createTabs()
	{
		TabControl form = maintenanceHistoryTab;
		form.addTab(historyEntries, createHistoryEntries());
		form.addTab(maintenanceTask, createMaintenanceTask());
		form.addTab(repair, createRepair());
		form.addTab(part, createPart());
		form.addTab(labor, createLabor());
		return form;
	}
	
	private void createTabFields(){
		maintenanceHistoryTab = new TabControl();
		historyEntries = new TabItem("History Entries");
		maintenanceTask = new TabItem("Maintenance Task");
		repair = new TabItem("Repair");
		part = new TabItem("Part");
		labor = new TabItem("Labor");
	}

	private void createLayout() {
		addField(createForm());
	}
	
	private GroupControl createGeneralTab() {
		GroupControl form = new GroupControl();
		form.addField(new jtk.project4.fleet.field.ButtonsMaintenanceHistoryField("Button Maintenance History"),NORTH);
		//form.addField(createGroupField(), SOUTH);
		form.addField(new DataIntervalSettingsField("Data Interval Settings"),WEST);
		form.addField(new TotalsField("Data Interval Settings"),EAST);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private GroupControl createHistoryEntries() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(maintenanceHistoryTable)
		.addRow()
		.addField(createGeneralTab());
		
		
		return form;
	}
	
private void createHistoryEntriesTable(){
		
		maintenanceHistoryTable = new TableField<History>("histories","history");
		//maintenanceHistoryTable.setTitle("MaintenanceHistory.header.table");
		maintenanceHistoryTable.setAutoResizeMode(AutoResizeMode.OFF);
		maintenanceHistoryTable.getSelectionValues();
		
		maintenanceHistoryTable.addColumn("label.date","dateI");
		maintenanceHistoryTable.addColumn("label.millage","units");
		maintenanceHistoryTable.addColumn("label.workPerformed","woNo");
		maintenanceHistoryTable.addColumn("label.totalCost","tCost");
		maintenanceHistoryTable.addColumn("label.downTime","downTime");
		maintenanceHistoryTable.addColumn("label.vendor","invNo");
		maintenanceHistoryTable.addColumn("label.costCenter","costCenter");
		maintenanceHistoryTable.addColumn("label.invoice","po");
		maintenanceHistoryTable.addColumn("label.maintenanceCost","pmCost");
		maintenanceHistoryTable.addColumn("label.repairCost","rCost");
		maintenanceHistoryTable.addColumn("label.partCost","pCost");
		maintenanceHistoryTable.addColumn("label.laborCost","lCost");
		maintenanceHistoryTable.addColumn("label.taxes","taxes");
		maintenanceHistoryTable.addColumn("label.comment","comments");
		
		maintenanceHistoryTable.sort("dateI", SortOrder.ASCENDING);
	}

	private GroupControl createMaintenanceTask() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new MaintenanceHistoryMaintenanceTaskTableField("MaintenanceTask"))
		//.addLabel("tabel maintenance task")
		.addRow()
		.addField(createGeneralTab());
		
		
		return form;
	}
	
	private GroupControl createRepair() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new MaintenanceHistoryRepairTableField("Repair"))
		//.addField(new MaintenanceHistoryTable("MaintenanceHistory"))
		//.addLabel("tabel Repair")
		.addRow()
		.addField(createGeneralTab());
		
		
		return form;
	}
	
	private GroupControl createPart() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new MaintenanceHistoryPartTableField("Part"))
		//.addField(new MaintenanceHistoryTable("MaintenanceHistory"))
		//.addLabel("tabel part")
		.addRow()
		.addField(createGeneralTab());
		
		
		return form;
	}
	private GroupControl createLabor() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new MaintenanceHistoryLaborTableField("Labor"))
		//.addField(new MaintenanceHistoryTable("MaintenanceHistory"))
		//.addLabel("tabel Labor")
		.addRow()
		.addField(createGeneralTab());
		
		
		return form;
	}

	
	private FormField createForm()
	{
		GroupControl form = new GroupControl();
		
		form.addField(createTabs());
		form.addField(createButtons(), SOUTH);
		
		return form;
	}
	
	private GroupControl createButtons(){
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(helpButton);
		buttons.addField(closeButton);
		return buttons;
	}
	
	
	
}
